<?php defined('SYSPATH') or die('No direct script access.');
/**
* Module Products Grouping (Categories, Subcategories, and Brands)
*/
class Module_Products_Productdiscount {
	
	private $controller;
	private $ss;
	private $thesystems;
	private $myfunctions;
	private $message;
	private $mydatetime;
	private $display;
	
	static function &factory() {
	   $o = new Module_Products_Productdiscount();
	  return $o;	
	}
	
	public function __construct()
	{
		$this->controller  = Request::current()->controller();
		$this->ss          = Session::instance();
		$this->thesystems  = Helper_theSystems::factory();
		$this->myfunctions = Helper_myFunctions::factory();
		$this->mydatetime  = Helper_MyDateTime::factory();
		$this->display		 = Helper_DisplayItems::factory();
		$this->message     = Language_Message::factory();
	}
	
	public function productdiscount($id, $command)
	{
		$rpp				= $this->thesystems->get_config('cms', 'pagination_medium_view');  // Record per page
		$halaman		= Arr::get($_GET,'page',1);
		$offset			= ($halaman - 1)*$rpp;
		$users_id		= $this->ss->get('logid');
		$discounttype	=	Arr::get($_REQUEST,'discounttype');
		
		switch ($command)
		{
		case "create":
				//rubah product_id(nama) dari autocomplete jadi (id)
				$product_id		= Arr::get($_REQUEST,'product_id');
				$hapus_text_stock = explode("(",$product_id); 
				$check_products = DB::select('id','name')
					->from('products')
					->where('name','=',$hapus_text_stock['0'])
					->execute()->as_array();
				if(!empty($check_products)) {
					$products_id = $check_products[0]['id'];
				} else {
					$this->message->store_message('c_fails');
				  $r = Request::current();
				  $r->redirect($this->controller.'/productdiscount');
				}
				//end rubah
				
				if($discounttype == 1) 
				{
				//rubah product_bonus(nama) dari autocomplete jadi (id)
				$prod_bonus		= Arr::get($_REQUEST,'prod_bonus');
				$hapus_text_stock = explode("(",$prod_bonus); 
				$check_products = DB::select('id','name')
					->from('products')
					->where('name','=',$hapus_text_stock['0'])
					->execute()->as_array();
				if(!empty($check_products)) {
					$prod_bonus = $check_products[0]['id'];
				} else {
					$this->message->store_message('c_fails');
				  $r = Request::current();
				  $r->redirect($this->controller.'/productdiscount');
				}
				//end rubah
				} else {
					$prod_bonus = NULL;
				}
				
			  $datetime					= $this->mydatetime->save_to_sql_datetime_now();
			  $product_id				= $products_id;
			  $discounttype			= Arr::get($_REQUEST,'discounttype');
			  $pcs							= Arr::get($_REQUEST,'pcs');
			  $prod_bonus				= $prod_bonus;
			  $prod_bonus_pcs		= Arr::get($_REQUEST,'prod_bonus_pcs');
			  $potongantype			= Arr::get($_REQUEST,'potongantype');
			  $potongan					= Arr::get($_REQUEST,'potongan');
			  $jml_bayar_item		= Arr::get($_REQUEST,'jml_bayar_item');
			  $approve					= Arr::get($_REQUEST,'approve');
			  
				$query = DB::insert('proddiscount', array('datetime','product_id','discounttype','pcs','prod_bonus','prod_bonus_pcs','potongan','potongantype','jml_bayar_item','approve'))
				  ->values(array($datetime,$product_id,$discounttype,$pcs,$prod_bonus,$prod_bonus_pcs,$potongan,$potongantype,$jml_bayar_item,$approve));
			 
				$result = $query->execute();
				if ($result[1] == 1) {
					$this->message->store_message('c_success');
				  $r = Request::current();
				  $r->redirect($this->controller.'/productdiscount');
				} else {
					$this->message->store_message('c_fails');
				}
				break;
		
		case "update":
					//rubah product_id(nama) dari autocomplete jadi (id)
					$product_id		= Arr::get($_REQUEST,'product_id');
					$hapus_text_stock = explode("(",$product_id); 
					$check_products = DB::select('id','name')
						->from('products')
						->where('name','=',$hapus_text_stock['0'])
						->execute()->as_array();
					if(!empty($check_products)) {
						$products_id = $check_products[0]['id'];
					} else {
						$this->message->store_message('c_fails');
					  $r = Request::current();
					  $r->redirect($this->controller.'/productdiscount');
					}
					//end rubah
					
					$prod_bonus		= Arr::get($_REQUEST,'prod_bonus');
					if(!empty($prod_bonus)) 
					{
					//rubah product_bonus(nama) dari autocomplete jadi (id)
					$hapus_text_stock = explode("(",$prod_bonus); 
					$check_products = DB::select('id','name')
						->from('products')
						->where('name','=',$hapus_text_stock['0'])
						->execute()->as_array();
					if(!empty($check_products)) {
						$prod_bonus = $check_products[0]['id'];
					} else {
						$this->message->store_message('c_fails');
					  $r = Request::current();
					  $r->redirect($this->controller.'/productdiscount');
					}
					//end rubah
					} else {
						$prod_bonus = NULL;
					}
					
				  $p['product_id']			= $products_id;
				  $p['pcs']							= Arr::get($_REQUEST,'pcs');
				  $p['prod_bonus']			= $prod_bonus;
				  $p['prod_bonus_pcs']	= Arr::get($_REQUEST,'prod_bonus_pcs');
				  $p['jml_bayar_item']	= Arr::get($_REQUEST,'jml_bayar_item');
				  $p['potongantype']		= Arr::get($_REQUEST,'potongantype');
				  $p['potongan']				= Arr::get($_REQUEST,'potongan');
				  $p['approve']					= Arr::get($_REQUEST,'approve');
				  
				  $query = DB::update('proddiscount')
					  ->set($p)
					  ->where('id', '=', $id);
				  $result = $query->execute();
					  if ($result == 1) {
							$this->message->store_message('u_success');
							$r = Request::current();
							$r->redirect($this->controller.'/productdiscount?id='.$id.'#openModal');
					  } else {
							$this->message->store_message('u_fails');
					  }
					break;
		
		case "delete":
					// Delete Product Group Data
				  $query = DB::delete('proddiscount')
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
							$this->message->store_message('d_success');
							$r = Request::current();
							$r->redirect($this->controller.'/productdiscount');
					  } else {
							$this->message->store_message('d_fails');
					  }
					break;
		}
		
		$count = DB::select()
			->from('proddiscount')
			->execute()
			->count();
		
		$productdiscounts = DB::select()
			->from('proddiscount')
			->limit($rpp)
			->offset($offset)
			->order_by('datetime', 'DESC')
			->execute()->as_array();
		
		$productdiscountupdate = DB::select()
			->from('proddiscount')
			->where('id', '=', $id)
			->limit(1)
			->execute();
		
		$pagination = Helper_myFunctions::factory()->paginations($count,$rpp);
		
		$content['title']		= 'Product Discount';
		$content['content']	= View::factory('inc/productdiscount')
			->set('productdiscounts',$productdiscounts)
			->set('productdiscountupdate',$productdiscountupdate[0])
			->set('pagination',$pagination)
			->render();
		
		return $content;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}